﻿using UnityEngine;

public class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
{
    private static T instance;
    private static object lockObject = new object();
    public static T Instance
    {
        get
        {
            lock (lockObject)
            {
                if(instance == null)
                {
                    instance = Transform.FindObjectOfType<T>();
                    if (Transform.FindObjectOfType<T>() == null)
                    {
                        instance = new GameObject(typeof(T).Name).AddComponent<T>();
                    }
                }
                return instance;
            }
        }
    }
}
